using System;
//using System.Net;
//using System.IO;
using System.Configuration;
using System.Collections.Specialized;
using OnlinePaymentProcessing.Paymentech;
//using Paymentech;
//using net.paymentech.ws;


namespace OnlinePayments
{
	/// <summary>
	/// Summary description for OrbitalGateway.
	/// </summary>
	internal class OrbitalGateway : PaymentProcessor
	{
		private string _merchantID;

		public OrbitalGateway( int paymentSystemID ) : base(paymentSystemID)
		{
			_merchantID = _configStrings["MerchantID"];

			if ( _merchantID == null )
				throw new Exception( "Missing data in the OrbitalGateway config section" );
		}

		public override bool Authorize( CustomerInfo customerInfo, 
			Int32 transactionID, 
			decimal amount, 
			out string responseCode,
			out string responseText,
			out string AVSResult,
			out string CVV2Response,
			out string processorTransactionID,
			out string authCode 
			)
		{
			//Transaction transaction = new Transaction(RequestType.CC_AUTHORIZE);
            NewOrderRequestElement transaction = new NewOrderRequestElement();

			string customerName = customerInfo.FirstName + " " + customerInfo.LastName;
			transaction.ccAccountNum = customerInfo.CardNumber;
			transaction.merchantID = _merchantID;
            transaction.terminalID = "001";
            transaction.industryType = "EC";
			transaction.bin = "000001";
			transaction.orderID = transactionID.ToString();
            transaction.customerRefNum = transactionID.ToString();
            transaction.profileOrderOverideInd = "OA";
            transaction.amount = (amount * 100).ToString();
			if ( customerInfo.CardExpiration == null || Convert.IsDBNull(customerInfo.CardExpiration) )
				transaction.ccExp = "    ";
			else
                transaction.ccExp = ((DateTime)customerInfo.CardExpiration).ToString("MMyy");
			transaction.avsName = 
				customerName.Substring( 0, Math.Min( 30, customerName.Length ) ).Trim();
            if (customerInfo.Address != null)
			    transaction.avsAddress1 = customerInfo.Address.Substring( 0, Math.Min( 30, customerInfo.Address.Length ) ).Trim();
            if (customerInfo.City != null)
    			transaction.avsCity =  customerInfo.City.Substring( 0, Math.Min( 20, customerInfo.City.Length ) ).Trim();
            if (customerInfo.State != null)
                transaction.avsState = customerInfo.State.Substring(0, Math.Min(2, customerInfo.State.Length)).Trim();
            if (customerInfo.Zip != null)
                transaction.avsZip = customerInfo.Zip.Substring(0, Math.Min(10, customerInfo.Zip.Length)).Trim();
            switch (customerInfo.Country.ToLower())
            {
                case "usa":
                    transaction.avsCountryCode = "US";
                    break;
                case "canada":
                    transaction.avsCountryCode = "CA";
                    break;
                default:
                    transaction.avsCountryCode = "";
                    break;
            }
            transaction.transType = "A";
			//transaction.TzCode = "705";
			//transaction.CurrencyCode = "840";
			//transaction.CurrencyExponent = "2";
			//transaction.TxDateTime = DateTime.Now.ToString("hhmmssMMddyyyy");
            transaction.ccCardVerifyNum = customerInfo.CVV2.Substring(0, Math.Min(3, customerInfo.CVV2.Length)).Trim();
            transaction.ccCardVerifyPresenceInd = (customerInfo.CVV2.Trim() == string.Empty) ? "9" : "1";

            PaymentechGatewayPortTypeClient gw = new PaymentechGatewayPortTypeClient();
            NewOrderResponseElement rsp = gw.NewOrder(transaction);

			responseCode = rsp.respCode;
			authCode = rsp.authorizationCode;
			AVSResult = rsp.avsRespCode;
			CVV2Response = rsp.cvvRespCode;
			responseText = rsp.respCodeMessage;
			processorTransactionID = rsp.txRefNum;

			return (rsp.approvalStatus == "1");
		}

		public override bool Deposit( CustomerInfo customerInfo, 
			Int32 transactionID, 
			decimal amount, 
			out string responseCode,
			out string responseText,
			out string AVSResult,
			out string CVV2Response,
			out string processorTransactionID 
			)
		{
            // We are currently only using the orbital gateway for authorizations.
            /*
			Transaction transaction = new Transaction(RequestType.CC_RECURRING_AUTH_CAPTURE);

			transaction["AccountNum"] = customerInfo.CardNumber;
			transaction["MerchantID"] = _merchantID;
			transaction["BIN"] = "000001";
			transaction["OrderID"] = transactionID.ToString();
			transaction["Amount"] = (amount * 100).ToString();
			if ( customerInfo.CardExpiration == null || Convert.IsDBNull(customerInfo.CardExpiration) )
				transaction["Exp"] = "    ";
			else
				transaction["Exp"] = ((DateTime)customerInfo.CardExpiration).ToString("MMyy");
			transaction["AVSname"] = customerInfo.FirstName + " " + customerInfo.LastName;
			transaction["AVSaddress1"] = customerInfo.Address;
			transaction["AVScity"] =  customerInfo.City;
			transaction["AVSstate"] = customerInfo.State;
			transaction["AVSzip"] = customerInfo.Zip;
			transaction["MessageType"] = "A";
			transaction["TzCode"] = "705";
			transaction["CurrencyCode"] = "840";
			transaction["CurrencyExponent"] = "2";
			transaction["TxDateTime"] = DateTime.Now.ToString("hhmmssMMddyyyy");

			Response rsp = transaction.Process();

			responseCode = rsp.ResponseCode;
			AVSResult = rsp.AVSResponseCode;
			CVV2Response = rsp.CVV2ResponseCode;
			responseText = rsp.Message;
			processorTransactionID = rsp.TxRefNum;

			return (rsp.Approved);
             */
			responseCode = string.Empty;
			responseText = string.Empty;
			AVSResult = string.Empty;
			CVV2Response = string.Empty;
            processorTransactionID = string.Empty;
            return false;
		}

		public override bool DepositWithPriorAuth( CustomerInfo customerInfo, 
			Int32 transactionID, 
			decimal amount, 
			string processorTransactionID,
			string authCode,
			out string responseCode,
			out string responseText,
			out string AVSResult,
			out string CVV2Response
			)
		{
            // We are currently only using the orbital gateway for authorizations.
            /*
			Transaction transaction = new Transaction(RequestType.CC_CAPTURE);

			transaction["AccountNum"] = customerInfo.CardNumber;
			transaction["MerchantID"] = _merchantID;
			transaction["BIN"] = "000001";
			transaction["OrderID"] = transactionID.ToString();
			transaction["Amount"] = (amount * 100).ToString();
			if ( customerInfo.CardExpiration == null || Convert.IsDBNull(customerInfo.CardExpiration) )
				transaction["Exp"] = "    ";
			else
				transaction["Exp"] = ((DateTime)customerInfo.CardExpiration).ToString("MMyy");
			transaction["TxRefNum"] = processorTransactionID;
			transaction["MessageType"] = "A";
			transaction["TzCode"] = "705";
			transaction["CurrencyCode"] = "840";
			transaction["CurrencyExponent"] = "2";

			Response rsp = transaction.Process();

			responseCode = rsp.ResponseCode;
			AVSResult = rsp.AVSResponseCode;
			CVV2Response = rsp.CVV2ResponseCode;
			responseText = rsp.Message;
			processorTransactionID = rsp.TxRefNum;

			return (rsp.Approved);
             */
            responseCode = string.Empty;
            responseText = string.Empty;
            AVSResult = string.Empty;
            CVV2Response = string.Empty;
            processorTransactionID = string.Empty;
            return false;
		}

		public override bool Refund( CustomerInfo customerInfo, 
			Int32 transactionID, 
			decimal amount, 
			string processorTransactionID,
			out string responseCode,
			out string responseText
			)
		{
            // We are currently only using the orbital gateway for authorizations.
            /*
			Transaction transaction = new Transaction(RequestType.CC_CAPTURE);

			transaction["AccountNum"] = customerInfo.CardNumber;
			transaction["MerchantID"] = _merchantID;
			transaction["BIN"] = "000001";
			transaction["OrderID"] = transactionID.ToString();
			transaction["Amount"] = (amount * 100).ToString();
			if ( customerInfo.CardExpiration == null || Convert.IsDBNull(customerInfo.CardExpiration) )
				transaction["Exp"] = "    ";
			else
				transaction["Exp"] = ((DateTime)customerInfo.CardExpiration).ToString("MMyy");
			transaction["TxRefNum"] = processorTransactionID;
			transaction["MessageType"] = "A";
			transaction["TzCode"] = "705";
			transaction["CurrencyCode"] = "840";
			transaction["CurrencyExponent"] = "2";

			Response rsp = transaction.Process();

			responseCode = rsp.ResponseCode;
			responseText = rsp.Message;
			processorTransactionID = rsp.TxRefNum;

			return (rsp.Approved);
             */
            responseCode = string.Empty;
            responseText = string.Empty;
            processorTransactionID = string.Empty;
            return false;
		}
	}
}
